import 'dart:async';

import 'package:flutter/material.dart';

import '../../api/home.dart';
import '../../components/Home_Nav.dart';
import '../../components/home_list.dart';
import '../../utils/Toast.dart';
import '../../utils/TokenManager.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List announceList = [];
  @override
  void initState() {
    super.initState();
    getAnnounceList();
  }

  getAnnounceList() async {
    final res = await getAnnounceListAPI();
    PromptAction.showSuccess('获取成功');
    setState(() {
      announceList = res;
    });
    // print(res);
  }

  int temers = 60;
  Timer? timer;
  Widget getcheshi() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: [
        ElevatedButton(
          onPressed: () async {
            await tokenManager.setToken('测试token123');
            PromptAction.showSuccess('token存储成功');
          },
          child: const Text('存储Token'),
        ),
        ElevatedButton(
          onPressed: () {
            timer = Timer.periodic(const Duration(seconds: 1), (timer) {
              setState(() {
                temers--;
              });
            });
            PromptAction.showSuccess('开启定时器');
          },
          child: const Text('开启定时器'),
        ),
        Text('剩余时间:$temers秒'),
        ElevatedButton(
          onPressed: () {
            timer?.cancel();
            PromptAction.showSuccess('关闭定时器');
          },
          child: const Text('关闭定时器'),
        ),
        ElevatedButton(
          onPressed: () {
            Future.delayed(
                const Duration(seconds: 3), (PromptAction.showSuccess('延时器')));
          },
          child: const Text('延时器'),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: const Color.fromARGB(40, 85, 145, 175),
        appBar: AppBar(
          title: const Text('享+社区'),
          backgroundColor: Colors.transparent,
          centerTitle: true,
        ),
        body: ListView(
          scrollDirection: Axis.vertical,
          padding: const EdgeInsets.all(10),
          children: [
            getcheshi(),
            // 导航条
            const HomeNav(),
            // 中间广告图
            ClipRRect(
              borderRadius: BorderRadius.circular(10.0),
              child: Image.asset('assets/images/banner@2x.jpg'),
            ),
            // 社区公告
            HomeList(notifyList: announceList)
          ],
        ));
  }
}
